/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListResponsePanel.java
 *
 * Created on Oct 14, 2010, 10:02:26 PM
 */

package edu.nus.iss.t5.jass.view;

import edu.nus.iss.t5.jass.controller.JobResponseController;
import edu.nus.iss.t5.jass.entity.JobResponse;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 * This class used to display Job Response List.
 * @author Fenglei
 */
public class ListResponsePanel extends javax.swing.JFrame {

      private JobResponseController controller;
      private JobResponseTableModel dm;

      /**
       * Constructor for ListResponsePanel
       * @param controller Job Response Controller.
       */
    public ListResponsePanel(JobResponseController controller) {

        dm=new JobResponseTableModel(null);

        initComponents();
      
        this.controller=controller;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtResponseList = new javax.swing.JTable();
        jbView = new javax.swing.JButton();
        jbClose = new javax.swing.JButton();

        jtResponseList.setModel(dm);
        jtResponseList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jtResponseList);

        jbView.setText("View Details");
        jbView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbViewActionPerformed(evt);
            }
        });

        jbClose.setText("Close");
        jbClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(441, Short.MAX_VALUE)
                .addComponent(jbView)
                .addGap(31, 31, 31)
                .addComponent(jbClose)
                .addGap(39, 39, 39))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbClose)
                    .addComponent(jbView))
                .addContainerGap(19, Short.MAX_VALUE))
        );
         this.setTitle("Job Response List");
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCloseActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jbCloseActionPerformed

    private void jbViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbViewActionPerformed
       int row=jtResponseList.getSelectedRow();
       if(row <0)
       {
           JOptionPane.showMessageDialog(this, "Please select a Job Response!","Selection",JOptionPane.OK_OPTION);
       }
       else
       {
           JobResponse response=dm.getRow(row);
           controller.showResposneDetailPanel(response);
       }
    }//GEN-LAST:event_jbViewActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbClose;
    private javax.swing.JButton jbView;
    private javax.swing.JTable jtResponseList;
    // End of variables declaration//GEN-END:variables



   /**
    * Display Job Response List.
    * @param responseList The Job Response List to be displayed.
    * @roseuid 4CAB38350253
    */
   public void ShowListResponses(List<JobResponse> responseList)
   {
         dm.removeAllRows();;
         dm.addRows(responseList);
         this.setVisible(true);
   }


   private class JobResponseTableModel extends AbstractTableModel
   {

        private List<JobResponse> jobResponseList;
        private int rows;
        private int columns;
        private  String headers[] = {"Candidate Name", "Job Code","Job Position" };
        public JobResponseTableModel(List<JobResponse> jobResponseList)
        {
            if(jobResponseList == null)
            {
                this.jobResponseList=new ArrayList<JobResponse>();
            }
            else
                this.jobResponseList=jobResponseList;
            rows=this.jobResponseList.size();
            columns=headers.length;
        }

        public int getRowCount() {
            return this.rows;
        }

        public int getColumnCount() {
            return this.columns;
        }
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Object objReturn =null;
            if(rowIndex <rows)
            {
                JobResponse response=this.jobResponseList.get(rowIndex);
                if(this.getColumnName(columnIndex).equals("Candidate Name"))
                {
                    objReturn=response.getJobSeeker().getName();
                }
                else if(this.getColumnName(columnIndex).equals("Job Code"))
                {
                    objReturn=response.getJobAdvertisement().getId();
                }
                 else if(this.getColumnName(columnIndex).equals("Job Position"))
                {
                    objReturn=response.getJobAdvertisement().getTitle();
                }
            }

            return objReturn;
        }


   @Override
  public String getColumnName(int column) {
    return headers[column];
  }

  public void removeAllRows()
  {
      this.jobResponseList.clear();
      rows=0;
      fireTableDataChanged();
  }

   public void addRow(JobResponse response)
   {
       this.jobResponseList.add(response);
       rows=rows+1;
       fireTableDataChanged();
   }

   public void addRows(List<JobResponse> jobResponseList)
   {
       for(JobResponse response : jobResponseList)
       {
           this.jobResponseList.add(response);
           rows=rows+1;
       }
       fireTableDataChanged();
   }
    public JobResponse getRow(int row)
    {
        return this.jobResponseList.get(row);
    }

   }
  
}
